{
  "cells": [
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "# OpenAltimetry ICESat-2: Photon ATL03\n\n\n### Overview\n<blockquote>This notebook demonstrates how to pull low level global geolocated photon data (ATL03) from the ICESat-2 mission via the OpenAltimetry API (https://openaltimetry.org/data/swagger-ui/).</blockquote>\n\n\n\n### Instructions\n<blockquote>Replace the \"OA_API_URL\" string with the data copied from : \"Get API request\" from the OpenAltimetry elevation chart and start visualizing your data!</blockquote>\n\nAuthor: Luis Lopez - luis.lopez@nsidc.org; updated: Minh Phan - mnphan@ucsd.edu"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "import pandas as pd\nimport json\nimport requests\nimport plotly.graph_objects as go\nfrom plotly.offline import iplot\n\n# Paste the OpenAltimetry API URL for Photon here: \nOA_API_URL = 'https://openaltimetry.org/data/api/icesat2/atl03?&minx=47.45681762695317&miny=33.116912841796854&maxx=47.59826660156254&maxy=33.229522705078104&date=2019-09-06&trackId=1081&beamName=gt3r&beamName=gt3l&beamName=gt2r&beamName=gt2l&beamName=gt1r&beamName=gt1l'\n\n# Select the list of confidence to display: 'Noise', 'Buffer', 'Low', 'Medium', 'High'\nOA_PHOTON_CONFIDENCE = ['Noise', 'Buffer', 'Low', 'Medium', 'High']\n\ndf_beams = []\noa_plots = []\n\n# This function will request the 6 beams data using OpenAltimetry's API\ndef getPhotonData():\n    series = []\n    b_url = OA_API_URL + '&client=jupyter'\n    print('Requesting data from OA')\n    r = requests.get(b_url)\n    data = r.json()\n    return data\n\n        \nphoton_cloud = getPhotonData()\n\nfor beam in photon_cloud:\n    \n    print('Reading data for beam: ' + beam['beam_name'])\n\n    for photons in beam['series']:\n        if any(word in photons['name'] for word in OA_PHOTON_CONFIDENCE):\n            series = []\n            for p in photons['data']:\n                series.append({\n                    'lat': p[0],\n                    'lon': p[1],\n                    'h': p[2]\n                })\n            if (len(series) > 0):\n                df = pd.DataFrame.from_dict(series)\n                df.name = beam['beam_name'] + ' ' + photons['name']\n                df_beams.append(df)\n\nfor df in df_beams:\n    oa_plots.append(go.Scatter3d(name=df.name, x=df['lat'], y=df['lon'], z=df['h'], mode='markers',\n                                    marker=dict(\n                                        size=1,\n                                        color=df['h'],\n                                        colorscale='Viridis',   # choose a colorscale\n                                        opacity=0.8\n                                    )\n                                )\n                   )\n    \nlayout = go.Layout(\n    width=900,\n    height=600,\n    scene = dict(aspectmode = 'manual', aspectratio =dict(x=1, y=1, z=0.5),\n                 xaxis=dict(title='Latitude'), yaxis=dict(title='Longitude'), zaxis=dict(title='Elevation (m)'))\n)\n\nprint('Plotting...')\n\nfig = go.Figure(data=oa_plots, layout=layout)  \n\niplot(fig)\n",
      "execution_count": 1,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# Now we can get a histogram of the beam's elevation and their description\nfor df in df_beams:\n    print(df.describe())\n    df.hist('h', bins=128)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python36",
      "display_name": "Python 3.6",
      "language": "python"
    },
    "language_info": {
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "name": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.6",
      "file_extension": ".py",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
